package DSRStructure;

import java.util.ArrayList;
import java.util.HashMap;
import reso.ip.IPAddress;

public class DSRRoutingEntry implements Cloneable{

	private ArrayList<IPAddress> route;
	

	public DSRRoutingEntry(){
		route = new ArrayList<>();
	}
	
	private void setRoute(ArrayList<IPAddress> route) {
		this.route = route;
	}

	public IPAddress getNextHop(IPAddress entry){
		try {
			for (int i = 0; i < route.size()-1 ; i++) {
				if(route.get(i) == entry){
					return route.get(i+1);
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}
	
	public IPAddress getPrevHop(IPAddress entry){
		try {
			for (int i = 1; i < route.size() ; i++) {
				if(route.get(i) == entry){
					return route.get(i-1);
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}
	
	public IPAddress getNeighborgHop(IPAddress entry){
		try {
			for (int i = 0; i < route.size() ; i++) {
				if(route.get(i) == entry){
					if(i == route.size()-1){
						return route.get(i-1);	
					}else{
						return route.get(i+1);
					}
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}
	
	public IPAddress getLastHop(){
		return route.get(route.size() - 1);
	}
	
	public IPAddress getFirstHop(){
		return route.get(0);
	}
	
	public boolean contains(IPAddress entry){
		return route.contains(entry);
	}
	
	public void addAddressEntry(IPAddress entry) {
		if(!route.contains(entry)){
			route.add(entry);
		}
	}
	
	public int getSize(){
		return route.size();
	}

	public DSRRoutingEntry reverse(){
		DSRRoutingEntry tempRoute = new DSRRoutingEntry();
		for (int i = route.size()-1; i >= 0; i--) {
			tempRoute.addAddressEntry(route.get(i));
		}
		return tempRoute;
	}
	
	@Override
	public String toString() {
		return "DSRRoutingEntry [route=" + route + "]";
	}

	public HashMap<IPAddress, DSRRoutingEntry> split(IPAddress currAddr) {
		HashMap<IPAddress, DSRRoutingEntry> tempList = new HashMap<IPAddress, DSRRoutingEntry>();
		int currPos = route.indexOf(currAddr);
		for (int i = 0; i < route.size(); i++) {
			DSRRoutingEntry tempRoute = new DSRRoutingEntry();
			if(i < currPos){
				for (int j = currPos; j >= i; j--) {
					tempRoute.addAddressEntry(route.get(j));
				}
				tempList.put(route.get(i), tempRoute);
				
			}else if (i > currPos){
				for (int j = currPos; j <= i; j++) {
					tempRoute.addAddressEntry(route.get(j));
				}
				tempList.put(route.get(i), tempRoute);
			}
		}
		return tempList;

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((route == null) ? 0 : route.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DSRRoutingEntry other = (DSRRoutingEntry) obj;
		if (route == null) {
			if (other.route != null)
				return false;
		} else if (!route.equals(other.route))
			return false;
		return true;
	}

	public DSRRoutingEntry clone() {
		DSRRoutingEntry temp = new DSRRoutingEntry();
		temp.setRoute((ArrayList<IPAddress>)route.clone());
		return temp;
	}



	

}
